﻿Imports System.IO
Imports System.Net
Imports System.Xml
Module CharacterUpdates
    'the update list grabbed from server
    Private CharacterUpdateList As String
    'the variables as they are sorted
    Private ListName(99), ListLevel(99), ListRace(99), ListClass(99), ListTime(99) As String
    'This is the header for all character update functions, send commands here
    Sub GetUpdatesList(Optional ByVal Type As String = "SaveCharacter")
        Dim wr As HttpWebRequest = CType(WebRequest.Create("http://www.theoestudio.com/plainsofsedia/CharacterUpdates.xml"), HttpWebRequest)
        Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
        Dim str As Stream = ws.GetResponseStream()
        Dim inBuf(100000) As Byte
        Dim bytesToRead As Integer = CInt(inBuf.Length)
        Dim bytesRead As Integer = 0
        Try
            While bytesToRead > 0
                Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)
                If n = 0 Then
                    Exit While
                End If
                bytesRead += n
                bytesToRead -= n
            End While
            str.Close()
            CharacterUpdateList = System.Text.Encoding.Unicode.GetString(inBuf)
            Dim test As String = Mid(CharacterUpdateList, 2, 2)
            If Mid(CharacterUpdateList, 2, 2) = " ?" Then
                CharacterUpdateList = System.Text.Encoding.Unicode.GetString(inBuf)
            End If
            SortUpdateList()
            If Type = "ShowLast5" Then
                'skip 0 as it's current entry
                Dim CurNum As Short = 1
                For CurNum = 1 To 5 Step 1
                    If ListName(CurNum) = "" Then : MainProgram.SND("/YList /Rended /Yshort.") : Exit For : End If 'if cur item is blank then stop recursion
                    MainProgram.SND("/Z" + ListName(CurNum) + "/w level " + ListLevel(CurNum) + " " + ListRace(CurNum) + " " + ListClass(CurNum) + " was updated on /y" + Replace(Replace(ListTime(CurNum), " +0000", ""), Right(Replace(ListTime(CurNum), " +0000", ""), 8), "") + "/Y" + Right(Replace(ListTime(CurNum), " +0000", ""), 8) + "/w.")
                Next
            ElseIf Type = "ShowLast10" Then
                'skip 0 as it's current entry
                Dim CurNum As Short = 1
                For CurNum = 1 To 10 Step 1
                    If ListName(CurNum) = "" Then : MainProgram.SND("/YList /Rended /Yshort.") : Exit For : End If 'if cur item is blank then stop recursion
                    MainProgram.SND("/Z" + ListName(CurNum) + "/w level " + ListLevel(CurNum) + " " + ListRace(CurNum) + " " + ListClass(CurNum) + " was updated on /y" + Replace(Replace(ListTime(CurNum), " +0000", ""), Right(Replace(ListTime(CurNum), " +0000", ""), 8), "") + "/Y" + Right(Replace(ListTime(CurNum), " +0000", ""), 8) + "/w.")
                Next
            ElseIf Type = "ShowLast15" Then
                'skip 0 as it's current entry
                Dim CurNum As Short = 1
                For CurNum = 1 To 15 Step 1
                    If ListName(CurNum) = "" Then : MainProgram.SND("/YList /Rended /Yshort.") : Exit For : End If 'if cur item is blank then stop recursion
                    MainProgram.SND("/Z" + ListName(CurNum) + "/w level " + ListLevel(CurNum) + " " + ListRace(CurNum) + " " + ListClass(CurNum) + " was updated on /y" + Replace(Replace(ListTime(CurNum), " +0000", ""), Right(Replace(ListTime(CurNum), " +0000", ""), 8), "") + "/Y" + Right(Replace(ListTime(CurNum), " +0000", ""), 8) + "/w.")
                Next
            Else
                If Type = "SaveCharacter" Then FormatNewList()
                If Type = "RemoveLast" Then removeitemlist()
                PutList()
                If Type = "SaveCharacter" Then PutCharacter()
            End If
        Catch errorvariable As Exception 'just incase file is corrupt or missing
            MainProgram.SND("/RERROR: " + errorvariable.ToString())
        End Try
    End Sub
    'This sorts the list gathered with getupdateslist
    Sub SortUpdateList()
        'declare xml dimensions
        Dim m_xmld As New XmlDocument
        Dim m_nodelist As XmlNodeList
        Dim m_node As XmlNode
        Dim CurNumber As Short
        m_xmld.LoadXml(CharacterUpdateList)
        Try
            m_nodelist = m_xmld.SelectNodes("/updatelist/item")
            CurNumber = 0
            For Each m_node In m_nodelist 'for each help in helplist
                CurNumber += 1
                ListName(CurNumber) = m_node.ChildNodes.Item(0).ChildNodes.Item(0).InnerText 'name
                ListLevel(CurNumber) = m_node.ChildNodes.Item(1).ChildNodes.Item(0).InnerText 'level
                ListRace(CurNumber) = m_node.ChildNodes.Item(2).ChildNodes.Item(0).InnerText 'race
                ListClass(CurNumber) = m_node.ChildNodes.Item(3).ChildNodes.Item(0).InnerText 'class
                ListTime(CurNumber) = m_node.ChildNodes.Item(4).ChildNodes.Item(0).InnerText 'time
            Next
            ListName(0) = MainProgram.CapitalizeEachWord(MainProgram.User_Name)
            ListLevel(0) = LTrim(Str(MainProgram.User_Level))
            ListRace(0) = MainProgram.User_Race
            ListClass(0) = MainProgram.User_Class
            ListTime(0) = Replace(WeekdayName(Date.Today.DayOfWeek), "day", "") + ", " + MainProgram.AddSpaceL(Date.Today.Day.ToString, 2, 1) + " " + Mid(MonthName(Date.Today.Month), 1, 3) + " " + Date.Today.Year.ToString + " " + TimeString + " +0000"
        Catch errorVariable As Exception
            MainProgram.SND("/RERROR: " + errorVariable.ToString())
        End Try
    End Sub
    'This formats the list by adding the new character save
    Sub FormatNewList()
        Dim CurNumber As Short
        CharacterUpdateList = "<?xml version=" + Chr(34) + "1.0" + Chr(34) + " encoding=" + Chr(34) + "UTF-8" + Chr(34) + "?>"
        CharacterUpdateList += Chr(13) + "<?xml-stylesheet type=" + Chr(34) + "text/xsl" + Chr(34) + " href=" + Chr(34) + "character.xsl" + Chr(34) + "?>"
        CharacterUpdateList += Chr(13) + "<!-- The character update list was first formated on 02/05/11 for use on www.theoestudio.com -->"
        CharacterUpdateList += Chr(13) + "<!-- Character Update List is meant to display the latest updated characters within PLOS and -->"
        CharacterUpdateList += Chr(13) + "<!-- display those characters on the main homepage of the game. The design was created by -->"
        CharacterUpdateList += Chr(13) + "<!-- Nathaniel Inman, all ideas, concepts, and data within are sole property of Nathaniel Inman -->"
        CharacterUpdateList += Chr(13) + "<!-- and The Other Experiment Studio respectfully. All rights reserved. -->"
        CharacterUpdateList += Chr(13) + "<!-- Note that the update list will only carry 100 items max -->"
        CharacterUpdateList += Chr(13) + "<updatelist>"
        For CurNumber = 0 To 99 Step 1
            If ListName(CurNumber) <> "" Then
                CharacterUpdateList += Chr(13) + "  <item number=" + Chr(34) + MainProgram.AddSpaceL(LTrim(Str(CurNumber + 1)), 3, 1) + Chr(34) + ">"
                CharacterUpdateList += Chr(13) + "      <name>" + ListName(CurNumber) + "</name>"
                CharacterUpdateList += Chr(13) + "      <level>" + ListLevel(CurNumber) + "</level>"
                CharacterUpdateList += Chr(13) + "      <race>" + ListRace(CurNumber) + "</race>"
                CharacterUpdateList += Chr(13) + "      <class>" + ListClass(CurNumber) + "</class>"
                CharacterUpdateList += Chr(13) + "      <time>" + ListTime(CurNumber) + "</time>"
                CharacterUpdateList += Chr(13) + "  </item>"
            Else
                Exit For
            End If
        Next
        CharacterUpdateList += Chr(13) + "</updatelist>"
    End Sub
    'This removes the last update from the list
    Sub removeitemlist()
        Dim CurNumber As Short
        CharacterUpdateList = "<?xml version=" + Chr(34) + "1.0" + Chr(34) + " encoding=" + Chr(34) + "UTF-8" + Chr(34) + "?>"
        CharacterUpdateList += Chr(13) + "<?xml-stylesheet type=" + Chr(34) + "text/xsl" + Chr(34) + " href=" + Chr(34) + "character.xsl" + Chr(34) + "?>"
        CharacterUpdateList += Chr(13) + "<!-- The character update list was first formated on 02/05/11 for use on www.theoestudio.com -->"
        CharacterUpdateList += Chr(13) + "<!-- Character Update List is meant to display the latest updated characters within PLOS and -->"
        CharacterUpdateList += Chr(13) + "<!-- display those characters on the main homepage of the game. The design was created by -->"
        CharacterUpdateList += Chr(13) + "<!-- Nathaniel Inman, all ideas, concepts, and data within are sole property of Nathaniel Inman -->"
        CharacterUpdateList += Chr(13) + "<!-- and The Other Experiment Studio respectfully. All rights reserved. -->"
        CharacterUpdateList += Chr(13) + "<!-- Note that the update list will only carry 100 items max -->"
        CharacterUpdateList += Chr(13) + "<updatelist>"
        For CurNumber = 2 To 99 Step 1
            If ListName(CurNumber) <> "" Then
                CharacterUpdateList += Chr(13) + "  <item number=" + Chr(34) + MainProgram.AddSpaceL(LTrim(Str(CurNumber - 1)), 3, 1) + Chr(34) + ">"
                CharacterUpdateList += Chr(13) + "      <name>" + ListName(CurNumber) + "</name>"
                CharacterUpdateList += Chr(13) + "      <level>" + ListLevel(CurNumber) + "</level>"
                CharacterUpdateList += Chr(13) + "      <race>" + ListRace(CurNumber) + "</race>"
                CharacterUpdateList += Chr(13) + "      <class>" + ListClass(CurNumber) + "</class>"
                CharacterUpdateList += Chr(13) + "      <time>" + ListTime(CurNumber) + "</time>"
                CharacterUpdateList += Chr(13) + "  </item>"
            Else
                Exit For
            End If
        Next
        CharacterUpdateList += Chr(13) + "</updatelist>"
    End Sub
    'This updates the list onto the server
    Sub PutList()
        Try
            Dim fwr As FtpWebRequest = DirectCast(WebRequest.Create("ftp://ftp.toes.0sites.org/plainsofsedia/CharacterUpdates.xml"), FtpWebRequest) 'make sure to replace \ with / since we're uploading
            fwr.Credentials = New NetworkCredential("5v3nd0ttg@toes.0sites.org", "p3n3l0py")
            fwr.Method = WebRequestMethods.Ftp.UploadFile
            Dim clsStream As System.IO.Stream = fwr.GetRequestStream()
            Dim bFile() As Byte = System.Text.Encoding.Unicode.GetBytes(CharacterUpdateList)
            clsStream.Write(bFile, 0, bFile.Length)
            clsStream.Close()
            clsStream.Dispose()
            fwr = Nothing
        Catch
            MainProgram.SND("/RERROR: " + Err.Description.ToString)
        End Try
    End Sub
    'This updates the character to the server that was just saved
    Sub PutCharacter()
        Try
            Dim fwr As FtpWebRequest = DirectCast(WebRequest.Create("ftp://ftp.toes.0sites.org/plainsofsedia/Characters/" + Replace(MainProgram.CapitalizeEachWord(MainProgram.User_Name), " ", "%20") + ".xml"), FtpWebRequest) 'make sure to replace \ with / since we're uploading
            fwr.Credentials = New NetworkCredential("5v3nd0ttg@toes.0sites.org", "p3n3l0py")
            fwr.Method = WebRequestMethods.Ftp.UploadFile
            Dim clsStream As System.IO.Stream = fwr.GetRequestStream()
            Dim bFile() As Byte = System.Text.Encoding.Unicode.GetBytes(MainProgram.User_Data)
            clsStream.Write(bFile, 0, bFile.Length)
            clsStream.Close()
            clsStream.Dispose()
            fwr = Nothing
        Catch
            MainProgram.SND("/RERROR: " + Err.Description.ToString)
        End Try
    End Sub
End Module
